package com.loong.news.controller.backend;

import com.loong.common.domain.BaseQuery;
import com.loong.common.domain.Pager;
import com.loong.common.mvc.BaseBackendController;
import com.loong.common.rest.Result;
import com.loong.common.validator.group.CommonInsert;
import com.loong.common.validator.group.CommonUpdate;
import com.loong.common.validator.group.CommonValidate;
import com.loong.news.common.backend.param.NewsCategoryParam;
import com.loong.news.common.backend.result.NewsCategoryResult;
import com.loong.news.handler.NewsCategoryHandler;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 处理新闻类目请求的controller
 * @Author: 刘群
 * @Date: 2020/10/14 15:22
 */
@RestController
@RequestMapping("manage/news/newsCategory")
public class NewsCategoryController extends BaseBackendController {

    @Resource
    private NewsCategoryHandler categoryHandler;

    /**
     * 新增新闻类别
     * @param param 新闻类别封装的参数
     * @return 响应的结果
     */
    @PostMapping("save")
    public Result saveNewsCategory(@Validated({CommonInsert.class}) NewsCategoryParam param){
        param.setId(null);
        return handlerExecutor.execute(() -> categoryHandler.saveNewsCategory(param));
    }

    /**
     * 更新新闻类别
     * @param param 新闻类别封装的参数
     * @return 响应的结果
     */
    @PostMapping("update")
    public Result updateNewsCategory(@Validated({CommonUpdate.class})NewsCategoryParam param){
        return handlerExecutor.execute(() -> categoryHandler.updateNewsCategory(param));
    }

    /**
     * 分页查询新闻类别
     * @param queryParam 查询参数
     * @return 响应的结果
     */
    @PostMapping("pageQuery")
    public Result pageQuery(BaseQuery queryParam){
        return handlerExecutor.execute(() -> categoryHandler.pageQuery(queryParam));
    }

    /**
     * 删除新闻类别
     * @param idList 新闻类别id集合
     * @return 响应的结果
     */
    @PostMapping("delete")
    public Result deleteNewsCategory(@RequestParam("ids")List<Long> idList){
        if (CollectionUtils.isEmpty(idList)) return null;
        return handlerExecutor.execute(() -> categoryHandler.deleteNewsCategory(idList));
    }

}
